﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Forms;

namespace UAS5
{
    /// <summary>
    /// 2013-6-5 by sutroon
    /// </summary>
    public partial class frmMain : Form
    {
        AppDataHelper adh;
        public frmMain()
        {
            InitializeComponent();
            this.MoveByControl_(this);

            cmiClickConfig();
            btnClickConfig();

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            adh = new AppDataHelper();
            ucIconTabControl1.uc_init();
            ucSoCommandLine1.uc_init();

            loadFavContextMenuItem();

            ucIconTabControl1.StartIconHandler += (string name, string path, string tag, Image img) =>
            {
                if (cmini最近打开ToolStripMenuItem.HasDropDownItems)
                {
                    if (cmini最近打开ToolStripMenuItem.DropDownItems.ContainsKey(name)) return;
                    if (cmini最近打开ToolStripMenuItem.DropDownItems.Count > 10) cmini最近打开ToolStripMenuItem.DropDownItems.RemoveAt(0);
                }
                ToolStripMenuItem cmi = new ToolStripMenuItem() { Text = string.Format("{0}(&{1})", name, name[0]), Tag = tag, Image = img, ToolTipText = path, Name = name };
                cmi.Click += new EventHandler(cmini_Click);
                cmini最近打开ToolStripMenuItem.DropDownItems.Add(cmi);
            };
        }
        /// <summary>
        /// 加载收藏菜单
        /// </summary>
        /// <remarks>2014-3-21 by sutroon</remarks>
        private void loadFavContextMenuItem()
        {
            cmini最近打开ToolStripMenuItem.DropDownItems.Clear();
            addClearFavContextMenuStripItem();
            DataTable dtFav = adh.SelectDataTable("tAppFavorite order by _id desc limit 10", "", "Name, Path, IconPath, Tags");
            if (dtFav.Rows.Count > 0)
            {
                string fn;
                foreach (DataRow r in dtFav.Rows)
                {
                    fn = r.Field<string>("Name");
                    ToolStripMenuItem cmi = new ToolStripMenuItem() { Text = string.Format("{0}(&{1})", fn, fn[0]), Tag = r.Field<string>("Tags"), Image = UCL.IO.FileHelper.GetImage(string.Format("{0}\\{1}", Application.StartupPath, r.Field<string>("IconPath"))), ToolTipText = r.Field<string>("Path"), Name = fn };
                    cmi.Click += new EventHandler(cmini_Click);
                    cmini最近打开ToolStripMenuItem.DropDownItems.Add(cmi);
                }
            }
        }
        // 添加默认收藏菜单
        private void addClearFavContextMenuStripItem()
        {
            ToolStripMenuItem cmi = new ToolStripMenuItem() { Text = "清空记录", Name = "cmini清空记录" };
            cmi.Click += (object objcmi, EventArgs ecmi) =>
            {
                adh.Delete("tAppFavorite", "");
                loadFavContextMenuItem();
            };
            cmini最近打开ToolStripMenuItem.DropDownItems.Add(cmi);
        }

        private void cmiClickConfig()
        {
            #region cmsOption
            cmiopt标签管理ToolStripMenuItem.Click += new EventHandler(cmsOption_Click);
            cmiopt关于ToolStripMenuItem.Click += new EventHandler(cmsOption_Click);
            cmiopt退出ToolStripMenuItem.Click += new EventHandler(cmsOption_Click);
            cmiopt清理痕迹ToolStripMenuItem.Click += new EventHandler(cmsOption_Click);
            cmiopt安装目录ToolStripMenuItem.Click += new EventHandler(cmsOption_Click);
            #endregion

            cmini主界面ToolStripMenuItem.Click += new EventHandler(cmini_Click);
            cmini退出ToolStripMenuItem.Click += new EventHandler(cmini_Click);
        }
        private void cmsOption_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem mi = (ToolStripMenuItem)sender;
            switch (mi.Name)
            {
                case "cmiopt标签管理ToolStripMenuItem":
                    new frmTagList().Show();
                    break;
                case "cmiopt关于ToolStripMenuItem":
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("快速拨号(UAS5)");
                    sb.AppendLine("本软件用于云应用的快速拨号，如您在办公室电脑与家庭电脑中都安装同一款软件，而两台电脑的安装路径不同，目前市面上还没有一款快速启动软件能解决这种情况的问题，因而开发了本软件，本软件每个拨号图标支持多个路径，启动的时候会自动检测有效的路径，从而正确启动程序。");
                    sb.AppendLine("本软件所有权归天澜(14507247@qq.com)及所拥有的公司(战图网络科技有限公司)");
                    sb.AppendLine("感谢您使用本产品，有什么建议或BUG请致信14507247@qq.com，万分感谢~");
                    MessageBox.Show(sb.ToString());
                    break;
                case "cmiopt退出ToolStripMenuItem":
                    Application.Exit();
                    break;
                case "cmiopt清理痕迹ToolStripMenuItem":
                    new frmDeleteRecords().Show();
                    break;
                case "cmiopt安装目录ToolStripMenuItem":
                    System.Diagnostics.Process.Start(Application.StartupPath);
                    break;
            }
        }

        private void cmini_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem cmi = (ToolStripMenuItem)sender;
            switch (cmi.Name)
            {
                case "cmini退出ToolStripMenuItem":
                    Application.Exit();
                    break;
                case "cmini主界面ToolStripMenuItem":
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                    break;
                default:
                    string msg = ucIconTabControl1.StartApp(cmi.ToolTipText, cmi.Tag.ToString());
                    if (msg != "SUCCESS") MessageBox.Show(msg);
                    break;
            }
        }

        private void btnClickConfig()
        {
            btnClose.Click += new EventHandler(btnClose_Click);
            btnOption.Click += new EventHandler(btnClose_Click);
            btnWindowMax.Click += new EventHandler(btnClose_Click);
            btnWindowMin.Click += new EventHandler(btnClose_Click);
        }
        // 退出程序
        private void btnClose_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btnClose":
                    this.Close();
                    break;
                case "btnOption":
                    cmsOptions.DisplayWithControl((Control)sender);
                    break;
                case "btnWindowMax":
                    this.WindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
                    break;
                case "btnWindowMin":
                    this.WindowState = FormWindowState.Minimized;
                    break;
            }
        }

        // 双击图标显示主界面
        private void niMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        // 点击关闭按钮最小化到任务栏图标
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //注意判断关闭事件Reason来源于窗体按钮，否则用菜单退出时无法退出!
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;    //取消"关闭窗口"事件
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
                return;
            }
        }


    }
}
